package cn.aitrox.ry.app.web.service.impl;

import cn.aitrox.ry.app.web.service.LesionService;
import cn.aitrox.ry.bean.CommonPageInfo;
import cn.aitrox.ry.common.service.BaseService;
import cn.aitrox.ry.service.config.dto.LesionDto;
import cn.aitrox.ry.service.config.dto.in.LesionSearchInDto;
import cn.aitrox.ry.service.config.feign.LesionFeign;
import cn.aitrox.ry.util.ArrayUtil;
import cn.aitrox.ry.util.BeanUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class LesionServiceImpl extends BaseService implements LesionService {

    @Autowired
    @Lazy
    private LesionFeign lesionFeign;

    @Override
    public Map<String, LesionDto> getAllLesionMap() {
        LesionSearchInDto in = new LesionSearchInDto();
        in.setPage(1);
        in.setPageSize(100000);
        CommonPageInfo<LesionDto> pageInfo = lesionFeign.lesionSearch(in);
        List<LesionDto> list = pageInfo.getList();
        if (ArrayUtil.isEmpty(list)) {
            return new HashMap<>();
        }
        Map<String, LesionDto> map = new HashMap<>();
        for (LesionDto lesion : list) {
            LesionDto newLesion = new LesionDto();
            BeanUtil.copy(lesion, newLesion);
            map.put(lesion.getValue(), newLesion);

            if (!ArrayUtil.isEmpty(lesion.getChilds())) {
                for (LesionDto childLesion : lesion.getChilds()) {
                    LesionDto newChildLesion = new LesionDto();
                    BeanUtil.copy(childLesion, newChildLesion);
                    map.put(childLesion.getValue(), newChildLesion);
                }
            }
        }
        return map;
    }
}
